/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import ejb.GameUser;
import ejb.UserFacadeRemote;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author automne
 */
public class UserControl {
    
    InitialContext context;
    UserFacadeRemote facade;

    public UserControl(String mappedName) {
        System.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
        try {
            context = new InitialContext();
            facade = (UserFacadeRemote) context.lookup(mappedName);
        } catch (NamingException ex) {
            Logger.getLogger(WordlistControl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void register(String username, String password, String realName) {
        GameUser newUser = new GameUser();
        newUser.setId(username);
        newUser.setPassword(password);
        newUser.setRealName(realName);
        newUser.setUserType("Player");
        facade.create(newUser);
    }

    public String userAuthentication(String username, String password) {
        String type = "";
        GameUser user = facade.find(username);
        if (user != null && user.getPassword().equals(password) == true) {
            type = user.getUserType();
        } 
        return type;
    }

    public GameUser findUser(String username) {
        return facade.find(username);
    }

    public void changePwd(GameUser user, String newPassword) {
        user.setPassword(newPassword);
        facade.edit(user);
    }
}
